import { filterRoutes } from "@/tools";

const GLOBAL = {
	CLEAR_STATE: "clearState",
	SET_COLLAPSE: "setCollapse",
	SET_MENU: "setMenu",
	SET_MENU_CHILD: "setMenuChild",
	SET_MENU_ROOT: "setMenuRoot"
};

const initialState = {
	collapse: false,
	menuChild: [],
	menuList: [],
	menuRoot: []
};

const state = () => ({ ...initialState });

const getters = {};

const mutations = {
	[GLOBAL.CLEAR_STATE](state) {
		Object.assign(state, initialState);
	},
	[GLOBAL.SET_COLLAPSE](state, data) {
		state.collapse = data;
	},
	[GLOBAL.SET_MENU](state, data) {
		state.menuList = data;
	},
	[GLOBAL.SET_MENU_CHILD](state, data) {
		state.menuChild = data;
	},
	[GLOBAL.SET_MENU_ROOT](state, data) {
		state.menuRoot = data;
	}
};

const actions = {
	// 清除数据
	clearState({ commit }) {
		commit(GLOBAL.CLEAR_STATE);
	},
	// 切换菜单
	setCollapse({ commit }, data) {
		commit(GLOBAL.SET_COLLAPSE, data);
	},
	// 设置菜单
	setMenu({ commit }, data) {
		commit(GLOBAL.SET_MENU, filterRoutes(data));
	},
	// 设置子菜单
	setMenuChild({ commit }, data) {
		commit(GLOBAL.SET_MENU_CHILD, data);
	},
	// 设置根菜单
	setMenuRoot({ commit }, data) {
		commit(GLOBAL.SET_MENU_ROOT, data);
	}
};

export default {
	actions,
	getters,
	mutations,
	namespaced: true,
	state
};
